package com.example.skudemo.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.skudemo.model.SpuEntity;
import com.example.skudemo.mapper.SpuMapper;
import com.example.skudemo.service.ISpuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.skudemo.utils.ApiResult;
import com.example.skudemo.vo.PageResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 商品spu表 服务实现类
 * </p>
 *
 * @author 水痕
 * @since 2023-11-17
 */
@Service
public class SpuServiceImpl extends ServiceImpl<SpuMapper, SpuEntity> implements ISpuService {
    @Autowired
    private SpuMapper spuMapper;

    @Override
    public ApiResult getSpuPageApi(Integer pageNumber, Integer pageSize) {
        IPage<SpuEntity> page = new Page<>(pageNumber, pageSize);
        IPage<SpuEntity> iPage = this.spuMapper.selectPage(page, null);
        PageResponseVO<SpuEntity> result = new PageResponseVO<>();
        result.setList(JSON.parseArray(JSON.toJSONString(iPage.getRecords()), SpuEntity.class));
        result.setPageNumber(pageNumber);
        result.setPageSize(pageSize);
        result.setTotal(iPage.getTotal());
        return ApiResult.success(result);
    }
}
